
	*Preamble;
		# d cr
		clear 
		set more off 
		clear matrix
		clear mata
		set mem 700m 
		set matsize 2000 
		set maxvar 32000
		cap log close
		# d ;

	*Set working directory;
		cd "C:\Users\Lorenzo Casaburi\Documents\InventoryCredit\3ie_report_analysis";

	*Local date of most recent databases;
		local data_date 20130625;
	*Local out_date;
		local out_date 20130625;
		
	*Call database;
		use dta/inventory_credit_final_data_`data_date', replace;
		des surveyid T*, fu;

	**Take_up table;	
		*Local independent variables for take-up regression to be shown in table;	
			local X_takeup  						
						B_e_qty_oil_peak 
						B_e_qty_oil_offpeak						
						 B_e1_masanakay_any 
						 B_e2d_red_any
						B_e_buy_for_sale						
						 B_e_advance10
						 B_b_gender_hh_head
						 B_i_disc_0_2
						 B_j_credit_constrained
						 B_h_CE						 
						 B_u_asset_score
						 B_near_mkt
						 B_near_road;
		
		
		*Standardize continuous variables;
			sum `X_takeup';
			foreach var of varlist
						B_e_qty_oil_peak 
						B_e_qty_oil_offpeak				
						 B_e_buy_for_sale
						 B_h_CE
						 B_i_disc_0_2						 						 
						 B_k6_save_50K
						 B_u_asset_score
						 B_near_mkt						 
						 B_near_road{;
				egen mean_var=mean(`var');
				egen sd_var=sd(`var');
				replace `var'=(`var'-mean_var)/sd_var;						 
				drop mean_var sd_var;
			};
			sum `X_takeup';
		
		*Create interactions with IC;	
			local X_takeup_all "";
			foreach var in `X_takeup'{;				
				cap gen `var'_IC=`var'*IC;
				local label_var: variable label `var';
				label var `var'_IC "$\quad \quad$*Inventory Credit";
				local X_takeup_all `X_takeup_all' `var' `var'_IC;
			};

		*Variable creation;
			tab T_takeup;
			gen T_nonzero_overall=T_overall_total_hh_takeup if T_takeup==1;
			tab TREAT T_takeup, row;

		*Regressions;
			local j=0;
			estimates clear;
			foreach y in T_takeup  T_nonzero_overall{;				
				*Title;
					if "`y'"=="T_takeup"{; local title Take-up Extensive Margin (0/1); local label tab-ext; };
					if "`y'"=="T_overall_total"{; local title Take-up Intensive Margin ; local label tab-int;};
					if "`y'"=="T_nonzero_overall"{; local title Take-up Intensive Margin (if $>$0) ; local label tab-int-nonzero ;};
				*Estimates clear;
					estimates clear;
				*1: y on ICS;
					local j=`j'+1;
					eststo:xi:reg `y'   ICS   if CONTROL==0, cluster(communityid);				
					estadd local p "" ;
				*2: y on ICS with controls;
					local j=`j'+1;
					eststo:xi:reg `y' ICS  `X_takeup' i.stratum_rand if CONTROL==0, cluster(communityid);																				
					estadd local p "" ;
				*3:y on ICS with controls and interactions;						
					foreach x of varlist `X_takeup'{;						
						*local j=`j'+1;			
						dis "`x'";	
						eststo:xi: reg `y' ICS  `x' `x'_IC i.stratum_rand  if CONTROL==0, cluster(communityid);													
						test `x'+`x'_IC=0;
						*return list;						
						local p1=round(r(p),.001);						
						local p1=string(`p1', "%9.3f");											
						dis "`p1'";
						estadd local pIC "`p1'";																																
					};
			*Esttab;
				esttab using "tex/table_take_up_`y'_`out_date'.tex",
				keep(ICS  `X_takeup_all' _cons)
				order(ICS  `X_takeup_all' _cons)
				b(%9.3f) se(%9.3f)
				scalars(						
						"pIC p-value IC "
						) 					
				nonotes				
				label star(* 0.1 ** 0.05 *** 0.01) replace obslast nomtitles compress longtable
				nonotes addnotes ("Notes: Robust Standard errors clustered at community level."
				"Regression includes dummies for each randomization stratum except for column (1)"
				"``p-value IC'' report the p-value for the sum of the level coefficient and the level  "
							"* p$<$0.1, ** p$<$0.05, *** p$<$0.01." 
							"Le.50000=USD12."	)
				nogaps			
				title(`title' \label{`label'});				
			};			
			
	cap log close;
	exit;
